package Server;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

public class ClientRepresentative extends Thread {
	private ServerSocket server;
	private Socket socket;
	private String name;
	private Vector<String> rooms;
	private ChatRooms cr;
	private boolean shallRun;

	public ClientRepresentative(ServerSocket server, Socket socket, ChatRooms cr) {
		this.server = server;
		this.socket = socket;
		rooms = new Vector<String>();
		this.cr = cr;
		shallRun = true;
	}

	public void run() {

		InputStream is = null;
		OutputStream os = null;

		try {
			is = socket.getInputStream();
			os = socket.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}

		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader bf = new BufferedReader(isr);
		boolean isAccepted = false;

		while (!socket.isClosed() && shallRun) {
			String line = "";
			try {
				line = bf.readLine();
				// System.out.println(line);
				if (line != null && line.length() > 0) {
					if (line.toCharArray()[0] == 'E') {
						// E, echo, for testing.
						try {
							line = line.substring(2, line.length());
							os.write(line.getBytes());
						} catch (IOException e) {
							e.printStackTrace();
						}
					} else if (line.toCharArray()[0] == 'M') {
						// M message:room
						int i = line.indexOf(':');
						String message = line.substring(2, i);
						String roomName = line.substring(i + 1, line.length());
						if (!roomName.equals("PrivateChat")) {
							message = "Protocol::SendMessageChatroom,"
									+ roomName + "," + name + ": " + message;
						} else {
							message = "Protocol::SendMessagePrivate,"
									+ roomName + "," + name + ": " + message;
						}
						cr.sendMessage(message, roomName, socket);

					} else if (line.toCharArray()[0] == 'Q') {
						// Q
						shallRun = false;
					} else if (line.toCharArray()[0] == 'J') {
						// J room
						line = line.substring(2, line.length());
						boolean succeded = cr.addPartToRoom(line, socket, name);
						cr.sendParticipants(socket, line);
						if (succeded) {
							rooms.add(line);
						}
					} else if (line.toCharArray()[0] == 'C') {
						// C room
						line = line.substring(2, line.length());
						boolean succeded = false;
						if (line.contains("PrivateChat")) {
							cr.createNewPrivate(line, socket, name);
						} else {
							succeded = cr.createNew(line, socket, name);
						}

						if (succeded) {
							rooms.add(line);
						}
					} else if (line.toCharArray()[0] == 'L') {
						// L room
						line = line.substring(2, line.length());
						cr.removeParticipantFromRoom(line, socket, name);
					} else if (line.contains("login")) {
						String[] lines = line.split(":");
						// login:username
						name = lines[1];
						isAccepted = cr.addNewParticipants(socket, name);
					} else if (line.contains("createPrivate")) {
						String[] lines = line.split(":");
						// createPrivate:room
						cr.createNewPrivate(lines[1], socket, name);
					} else if (line.contains("getRooms") && line.length() == 8) {
						// getRooms
						cr.sendRooms(socket);
					} else if (line.contains("getPart")) {
						// getPart:room
						String[] temp = line.split(":");
						cr.sendParticipants(socket, temp[1]);
					} else if (line.contains("getAllNames")) {
						cr.sendAllNames(socket);
					} else if (line.contains("updateAllnames")) {
						cr.updateAllNames();
					} else if (line.contains("Filetransfer")) {
						String[] temp = line.split(",");
						cr.sendFileRequest(socket, temp[1]);
					} else if (line.contains("FileNotAccepted")) {
						String[] temp = line.split(",");
						cr.sendFileNotAccepted(socket, temp[1]);
					} else if (line.contains("FileAccepted")) {
						String[] temp = line.split(",");
						cr.sendFileAccepted(socket, temp[1], temp[2]);
					}

				}
			} catch (IOException e1) {
				shallRun = false;
			}

		}
		if (isAccepted) {
			for (String s : rooms) {
				cr.removeParticipantFromRoom(s, socket, name);
			}
			cr.removeParti(socket, name);
		}
		try {
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
